package co.yixiang.modules.shop.web.controller;

import co.yixiang.modules.shop.entity.YxInvestCustomer;
import co.yixiang.modules.shop.entity.YxSales;
import co.yixiang.modules.shop.service.YxInvestCustomerService;
import co.yixiang.modules.shop.service.YxSalesManageService;
import co.yixiang.modules.shop.service.YxSalesService;
import co.yixiang.modules.shop.service.YxSystemConfigService;
import co.yixiang.modules.shop.web.param.YxInvestCustomerQueryParam;
import co.yixiang.modules.shop.web.vo.YxInvestCustomerQueryVo;
import co.yixiang.common.web.controller.BaseController;
import co.yixiang.common.api.ApiResult;
import co.yixiang.modules.shop.web.vo.YxSalesManageQueryVo;
import co.yixiang.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import co.yixiang.common.web.vo.Paging;
import co.yixiang.common.web.param.IdParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 投资客户表 前端控制器
 * </p>
 *
 * @author hisen
 * @since 2020-06-15
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "客户模块", tags = "投资端:客户模块", description = "客户模块")
public class YxInvestCustomerController extends BaseController {

    @Autowired
    private YxInvestCustomerService yxInvestCustomerService;
    private final YxSalesService yxSalesService;
    private final YxSalesManageService yxSalesManageService;


    /**
     * 投资客户表分页列表
     */
    @GetMapping("/customer/list")
    @ApiOperation(value = "客户列表(个人)",notes = "客户列表(个人)")
    public ApiResult<List<YxInvestCustomerQueryVo>> customerList(YxInvestCustomerQueryParam queryParam) throws Exception{
        int uid = SecurityUtils.getUserId().intValue();
        return ApiResult.ok(yxInvestCustomerService.customerList(uid, queryParam.getPage().intValue(),queryParam.getLimit().intValue(),null));
    }


    /**
     * 投资客户表分页列表
     */
    @GetMapping("/teamCustomer/list")
    @ApiOperation(value = "客户列表(团队)",notes = "客户列表(团队)")
    public ApiResult<List<YxInvestCustomerQueryVo>> teamCustomerList(YxInvestCustomerQueryParam queryParam) throws Exception{
        List<Integer>  saleIdList = null;
        int uid = SecurityUtils.getUserId().intValue();
        YxSales yxSales = yxSalesService.getById(uid);
        List<YxSalesManageQueryVo> saleList = yxSalesManageService.getListByManagerId(yxSales.getUid());
        if(yxSales.getGroupId() == 2){
            //如果是经理，查询他负责的业务员
            saleList = yxSalesManageService.getListByManagerId(yxSales.getUid());
            if(saleList != null && saleList.size() > 0){
                saleIdList = new ArrayList<>();
                for(YxSalesManageQueryVo yxSalesManageQueryVo : saleList){
                    saleIdList.add(yxSalesManageQueryVo.getUid());
                }
            }
        }

        else if(yxSales.getGroupId() == 4){
            uid =0;
        }

        if(queryParam.getSalesId() != -1){
            uid = queryParam.getSalesId();
        }

        return ApiResult.ok(yxInvestCustomerService.customerList(uid, queryParam.getPage().intValue(),queryParam.getLimit().intValue(),saleIdList));
    }
}

